package com.icss.daos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.icss.beans.DynamicNewsVO;
import com.icss.beans.HotNewsVO;
/**
 * 
 * @author 梁海成
 *类作用：最新动态业务操作管理
 */

public class DynamicNewsDao {
	Dao dao = new Dao();

	ResultSet rs = null;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
/**
 * 关闭连接
 */
	public void close() {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dao.close();

		}
		dao.close();
	}
	
	/**
	 * 获取最新动态的所有文章
	 */
	public ArrayList<DynamicNewsVO> getAllNews(){
		String sql = "select * from DynamicNews";
		ArrayList<DynamicNewsVO> list = new ArrayList<DynamicNewsVO>();
		rs= dao.query(sql);
		try {
			while(rs.next()){
				DynamicNewsVO vo = new DynamicNewsVO();
				vo.setDynamicNewsContext(rs.getString("dynamicNewsContext"));
				vo.setDynamicNewsID(rs.getInt("dynamicNewsID"));
				vo.setDynamicNewsTitle(rs.getString("dynamicNewsTitle"));
				vo.setPublishTime(sdf.format(rs.getDate("publishTime")));
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	/**
	 * 获取在首页显示的文章
	 */
	public ArrayList<DynamicNewsVO> getIndexNews(){
		String sql = "select * from DynamicNews limit 0,3";
		ArrayList<DynamicNewsVO> list = new ArrayList<DynamicNewsVO>();
		rs= dao.query(sql);
		try {
			while(rs.next()){
				DynamicNewsVO vo = new DynamicNewsVO();
				vo.setDynamicNewsContext(rs.getString("dynamicNewsContext"));
				vo.setDynamicNewsID(rs.getInt("dynamicNewsID"));
				vo.setDynamicNewsTitle(rs.getString("dynamicNewsTitle"));
				vo.setPublishTime(sdf.format(rs.getDate("publishTime")));
				list.add(vo);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	
	/**
	 * 添加文章
	 */
	public void addNews(String title,String context){
		String sql = "insert into DynamicNews (dynamicNewsTitle,dynamicNewsContext) values('"+title+"','"+context+"')";
		dao.execute(sql);
		close();
	}
	
	/**
	 * 根据文章ID查询相应的文章
	 */
	public DynamicNewsVO getOneNews(int id){
		String sql ="select * from DynamicNews where dynamicNewsID="+id;
		rs=dao.query(sql);
		DynamicNewsVO vo = new DynamicNewsVO();
		
		try {
			if(rs.next()){
				vo.setDynamicNewsContext(rs.getString("dynamicNewsContext"));
				vo.setDynamicNewsID(rs.getInt("dynamicNewsID"));
				vo.setDynamicNewsTitle(rs.getString("dynamicNewsTitle"));
				vo.setPublishTime(sdf.format(rs.getDate("publishTime")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return vo;
	}
	
	/**
	 * 更新相应的文章
	 */
	public void updateNews(int id,String title,String context){
		String sql = "update DynamicNews set dynamicNewsTitle='"+title+"',dynamicNewsContext='"+context+"' where dynamicNewsID="+id;
		dao.execute(sql);
		close();
	}
	/**
	 * 根据文章ID删除文章
	 */
	public void deleteNews(int id){
		String sql ="delete from DynamicNews where dynamicNewsID="+id;
		dao.execute(sql);
		close();
	}
	
	/**
	 * 批量删除文章
	 */
	public void delAllSelect(String str){
		String[] arr = str.split(",");
		
		for(int i=0;i<arr.length;i++){
			String sql = "delete from DynamicNews where dynamicNewsID="+Integer.parseInt(arr[i]);			
			dao.execute(sql);
		}
		close();
	}
	/**
	 * 获取不是给定ID的文章的其它文章
	 */
	public List<DynamicNewsVO> getotherNews(int id){
		String sql = "select * from DynamicNews where dynamicNewsID<>"+id + " limit 0,8";
		List<DynamicNewsVO> list = new ArrayList<DynamicNewsVO>();
		Dao dao = new Dao();
		rs=dao.query(sql);
		try {
			while(rs.next()){
				DynamicNewsVO vo = new DynamicNewsVO();
				vo.setDynamicNewsContext(rs.getString("dynamicNewsContext"));
				vo.setDynamicNewsID(rs.getInt("dynamicNewsID"));
				vo.setDynamicNewsTitle(rs.getString("dynamicNewsTitle"));
				vo.setPublishTime(sdf.format(rs.getDate("publishTime")));
				list.add(vo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close();
		}
		return list;
	}
	
}
